package net.angrynut.web.handler.controller;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.google.common.collect.ImmutableMap;

import net.angrynut.web.model.dto.LoginForm;

/**
 * 演示 Java 校验注解（JSR303 Bean Validation）。参考 Spring Guides
 * <a href="https://spring.io/guides/gs/validating-form-input/">Validating Form
 * Input</a>
 */
@RestController
public class DemoValidController {

	@PostMapping("/demo/valid")
	public Object valid(@RequestBody @Valid LoginForm form, BindingResult br) {
		if (br.hasErrors()) {
			return br.getAllErrors();
		}
		return ImmutableMap.of("resule", "OK");
	}

	@PostMapping("/demo/validated")
	public Object validated(@RequestBody @Validated LoginForm form, Errors errors) {
		if (errors.hasErrors()) {
			return errors.getAllErrors();
		}
		return ImmutableMap.of("resule", "OK");
	}
}
